﻿@model RBAC.Web.Models.PermissionModel
@using RBAC.Web.CustomHelpers
@using RBAC.Web.Mapper
@using RBAC.Web.Utilities

@{
    var userProfile = Session["UserProfile"] as RbacLib.Models.UserProfile;
    var privilageInsertPermission = RbacUtil.SetAddPermissionButton(userProfile);

    //privilageInsertPermission = string.Empty;
}

@{
    ViewBag.Title = "Add Permission";
    Layout = "~/Views/Shared/_Layout.cshtml";

    string returnUrl = "";
    string RoleCode = "";
    if (TempData["RoleLookupField"] == null || String.IsNullOrEmpty(TempData["RoleLookupField"].ToString()))
    {
        returnUrl = Url.Action("ListPermission", "Permission");
    }
    else
    {
        RoleCode = TempData["RoleLookupField"].ToString();
        returnUrl = Url.Action("EditRole", "Role", new { RoleCode = RoleCode });
    }  
																																												 
}

@section Styles {
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/form.js")" type="text/javascript"></script>
    <link href="@Url.Content("~/Content/themes/base/jquery-ui.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/themes/base/jquery.ui.datepicker.css")" rel="stylesheet" type="text/css" />

    <script type="text/javascript">
        $(function () {
            //How to setup Datepicker
            // SetDatePicker has 4 argurement
            // controlId -> id of control
            // maxdate -> maximun Date that can be selected 
            // mindate -> minimum Date that can be selected 
            // yearrange - > range of year
            // more information see http://api.jqueryui.com/datepicker/
            SetDatePicker("CreateDate", new Date(), '-71Y', 'c-70:c');
            //ถ้าไม่มีกำหนด maxdate, mindate, yearrange
            SetDatePicker("FreeDate");			 

            //How to setup Lookup
            // SetLookupFor has 3 argurement
            // controlId -> id of control
            // getLookupId -> hidden control's id for getlookup link
            // popLookupId -> hidden control's id for poplookup link
            SetExternalLookupFor("Permission_Role", "Permission_RoleGetExternalLookup", 
"Permission_RolePopExternalLookup", 'Permission_Role|Permission_RoleLookupField|RoleId');
			
            SetExternalLookupFor("Permission_Function", "Permission_FunctionGetExternalLookup", 
"Permission_FunctionPopExternalLookup", 'Permission_Function|Permission_FunctionLookupField|FunctionId');
			
            SetExternalLookupFor("Permission_RoleLookup", "Permission_RoleLookupGetExternalLookup", 
"Permission_RoleLookupPopExternalLookup", 'Permission_RoleLookup|Permission_RoleLookupLookupField|RoleLookupId');
			
        });
    </script>
}

<h2>New Permission</h2>


@using (Html.BeginForm("AddPermission", "Permission", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.ValidationSummary(false)
    <fieldset>
        <legend>PermissionModel</legend>

        @Html.Hidden("Parent_RoleRoleCode", @RoleCode)

        <div class="two-column-container">


            <div class="texteditor" >
                @Html.LabelFor(model => model.PermissionName)
                @Html.EditorFor(model => model.PermissionName)
                <input type="hidden" name="Mode" value="Add" />
            </div>


            <div class="texteditor">
                @Html.LabelFor(model => model.Description)
                @Html.EditorFor(model => model.Description)
            </div>




            <div class="texteditor">
                @Html.LabelFor(model => model.CreateDate)
                @Html.DatePickerFor(model => model.CreateDate, "CreateDate",true)
            </div>


            @* START ถ้ามี lookup ไปหาแม่ ClildClass=True, LinkToParent=True *@
          
         @*   chayawooth.puy*@
             @* <div class="texteditor">
                @Html.LabelFor(model => model.RoleId)
                @Html.TextBoxFor(model => model.RoleId, new { @readonly = "readonly", @class = "txtFillNameExternal" })

            </div>*@

            <div class="texteditor">
                @Html.LabelFor(model => model.Role)
                @Html.ExternalLookup(model => model.RoleRoleCode, "Permission_Role").For(model => model.Role).Render(Utilities.CheckNullOrEmpty<string>(RoleCode))
            </div>
            @* END ถ้ามี lookup ไปหาแม่ ClildClass=True, LinkToParent=True *@




            @* START ถ้าไม่มี lookup ไปหาแม่ ClildClass=True, LinkToParent=False *@
           
           @*chayawooth.puy*@
             @*<div class="texteditor">
                @Html.LabelFor(model => model.FunctionId)
                @Html.TextBoxFor(model => model.FunctionId, new { @readonly = "readonly", @class = "txtFillNameExternal" })
            </div>*@

            <div class="texteditor">
                @Html.LabelFor(model => model.Function)
                @Html.ExternalLookup(model => model.FunctionFuncName, "Permission_Function").For(model => model.Function).Render()
            </div>
            @* END ถ้าไม่มี lookup ไปหาแม่ ClildClass=True, LinkToParent=False *@



            <div class="texteditor">
                @Html.LabelFor(model => model.PermissionStatus)
                @Html.RadioButtonForSelectList(model => model.PermissionStatus, PermissionStatusMapper.GetPermissionStatusSelectList())
            </div>

        </div>
        <div class="bottom-menu clear">
            <input type="submit" value="Add" @privilageInsertPermission />
            <input id="cancel" onclick="location.href='@returnUrl    '" type="button" value="Cancel" />
        </div>
    </fieldset>
}
